package KnightsTour;

/**
 *
 * @author Scott Robinson
 */
public class Knight extends ChessPiece {
    private final int validMoves [][] = {{-1,2},{-2,1},{-2,-1},{-1,-2},
                                        {1,-2},{2,-1},{2,1},{1,2}};

    public Knight (ChessBoard cb , int row, int column) {
        super (cb, row, column);
    }

    @Override
    public boolean move(int r, int c) {
        if (getCb().isOnBoard(r, c) && isValidKnightMove(r, c) && !getCb().getChessBoardSquare(r, c).wasVisited()) {
            setRow(r);
            setColumn(c);
            return true;
        }
        else return false;
    }

    private boolean isValidKnightMove( int r , int c) {
        for (int moves [] : validMoves) {
            if (moves [0] == (r - getRow()) && moves [1] == c - getColumn()) return true;
        }
        return false;
    }

    @Override
    public boolean hasMoreMoves() {
        for (int moves[] : validMoves) {
            if (getCb().isOnBoard(getRow() + moves[0] , getColumn() + moves[1])) {
                if (isValidKnightMove(getRow() + moves[0], getColumn() + moves[1])) {
                    if (!getCb().getChessBoardSquare(getRow() + moves[0], getColumn() + moves[1]).wasVisited()) {
                        return true;
                    }
                }
            }
            }
        return false;
        }
    }

